package com.hxzy.controller;

import com.hxzy.pojo.Emp;
import com.hxzy.pojo.Result;
import com.hxzy.service.EmpService;
import com.hxzy.utils.JwtUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wzm
 * @create 2024-03-07 15:33
 * project_name:JavaWeb2401
 * @Description
 */
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    //员工登录的方法
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){

       Emp e =  empService.login(emp);
       if (e != null){
           //登录成功
           Map<String,Object> map = new HashMap<>();
           map.put("id",e.getId());
           map.put("username",e.getUsername());
           map.put("name",e.getName());

           //生成jwt
           String jwt = JwtUtils.getJwt(map);

           //响应
           return Result.success(jwt);
       }
       return Result.error("用户名或密码错误，登录失败");





    }

}
